<template>
  <div class="login">
    <div class="login-box">
      <!-- 头像区域 -->
      <div class="login-header">
        <img src="../assets/img/yuzi.jpg" alt="" />
      </div>

      <!-- 表单区域 -->
      <el-form label-width="0px"  class="login-form" :model="loginForm" :rules="loginFormRules" ref="loginFormRef">
        <el-form-item  prop="username">
          <el-input prefix-icon="iconfont icon-yonghu"
            v-model="loginForm.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item  prop="password">
          <el-input type="password" prefix-icon="iconfont icon-suo" show-password
            v-model="loginForm.password"
            
          ></el-input>
        </el-form-item>

        <el-form-item class="btns">
          <el-button type="primary" @click="submitForm('loginFormRef')"
            >提交</el-button
          >
          <el-button @click="resetForm('loginFormRef')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        // 登录表单数据
        loginForm:{
          username:'admin',
          password:'123456'
        },
        //  登录表单验证规则
        loginFormRules:{
          username:[
            { required: true, message: '请输入用户名' ,triger:blur}
          ],
          password:[
            { required: true, message: '请输入密码' ,triger:blur},
            {min:3,max:8,message: '长度在 3 到 6 个字符', trigger: 'blur' }
          ]
      }
      };
    },
    methods: {
      // 提交
      submitForm(refName) {
        this.$refs[refName].validate(async valid => {
          if (!valid) return
          const {data:res} = await this.$http.post('login',this.loginForm);
          console.log(res.data);
          if(res.meta.status !== 200) return this.$message.error('登录失败');
          // 文本提示框
          this.$message.success('登录成功');
          // sessionStorage 存储 token
          window.sessionStorage.setItem('token',res.data.token);
          this.$router.push('/home');// 跳转到首页
        });
      },
      // 重置
      resetForm(refname) {
        // console.log(this);
        // element-ui 的 resetFields 方法 将表单的数据恢复默认值、规则提示清空
        this.$refs[refname].resetFields();
      }
    }
  }
</script>

<style scoped>
.login {
  position: relative;
  height: 100%;
  background-color: #2b4b6b;
}
.login-box {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 450px;
  height: 300px;
  /* margin-left: -225px;
  margin-top: -150px; */
  background-color: #fff;
  border-radius: 8px;
  transform: translate(-50%, -50%);
}

.login-header {
  position: absolute;
  top: -65px;
  left: 50%;
  width: 130px;
  height: 130px;
  margin-left: -65px;
  padding: 10px;
  border: 1px solid #eee;
  border-radius: 50%;
  box-shadow: 0 0 10px #ddd;
  background-color: #dae2eb;
}

.login-header img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
}

.login-form {
  box-sizing: border-box;
  padding:0 100px;
  margin-top: 100px;
}
.btns {
  box-sizing: border-box;
  width: 100%;
  padding: 0 45px;

}
</style>
